<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../js/vue.js"></script>
    <title>列表排序</title>
</head>
<body>
    <!-- 
        
     -->
    <div id="app">
        <h1>人员列表</h1>
        <input type="text" placeholder="请输入姓名关键字" v-model="name">
        <button @click="sortType=2">年龄升序</button>
        <button @click="sortType=1">年龄降序</button>
        <button @click="sortType=0">原顺序</button>
        <ul>
            <li v-for="p,index in finalPersons" :key='p.id'>
                {{p.name}}-{{p.age}}-{{p.sex}}
            </li>
        <b v-if="!finalPersons.length">没有人员数据</b>
        </ul>
       
    </div>
    <script>
        Vue.config.productionTip = false
        const vm = new Vue({
            data(){
                return {
                   name:'',
                   sortType: 0,  // 0原顺序  1降序   2升序
                   list:[
                       {id:'001',name:'马冬梅',age:18,sex:'女'},
                       {id:'002',name:'周冬雨',age:17,sex:'女'},
                       {id:'003',name:'周杰伦',age:20,sex:'女'},
                       {id:'004',name:'郭艾伦',age:21,sex:'男'},
                       {id:'005',name:'温兆伦',age:19,sex:'男'},
                   ],
                   persons:[]
                }
            },
            methods: {
                
            },
            computed:{
                finalPersons(){
                    // 数组filter不改变原数组， sort改变原数组
                    const arr = this.list.filter(item=>{
                        return item.name.indexOf(this.name) !== -1
                    })
                    if(this.sortType){
                        arr.sort((a,b)=>{
                            return this.sortType==2 ? a.age-b.age : b.age-a.age
                        })
                    }
                    return arr
                }
            }
        }).$mount('#app')
    </script>
</body>
</html>